package com.leon.alishop.core.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.leon.alishop.core.config.ProviderConstant;
import com.leon.alishop.core.service.ProviderInfoService;
import com.leon.alishop.core.vo.ProviderParams;
import com.leon.alishop.dao.ProviderInfoDao;
import com.leon.alishop.model.ProviderInfo;

@Service("providerInfoService")
public class ProviderInfoServiceImpl implements ProviderInfoService{

	@Override
	public int insertOrUpdateProviderInfo(ProviderParams params) {
		if(params == null)
			return 0;
		int ret = 0;
		ProviderInfo pro = ((ProviderInfo)params).clone();
		ProviderInfo proOri = providerInfoDao.findByTaobaoHomeUrl(pro.getTaobaoHomeUrl());
		if(null == pro.getId()){
			if(null != proOri){
				params.setErrorMsg("该淘宝网址已经添加！");
				return 0;
			}else{
				ret = providerInfoDao.insertSelective(pro);
				params.setId(pro.getId());
				ProviderConstant.getInstance().reload();
				return ret;
			}
		}else{
			if(proOri != null && !proOri.getId().equals(pro.getId())){
				params.setErrorMsg("该淘宝网址已经添加！");
				return 0;
			}
			ProviderConstant.getInstance().reload();
			return providerInfoDao.updateByIdSelective(pro);
		}
	}

	@Autowired
	private ProviderInfoDao providerInfoDao;
}
